// TODO Déplacer en JSOn ?
// Shaders descriptions
var shadersDescriptions = [
    {
        name: "shader_default",
        isTextureCoordAttribute: false,
        isVertexNormalAttribute: false,
        isLightings: false,
        vsContent: vsDefaultContent,
        fsContent: fsDefaultContent
    },
    
    {
        name: "shader_lights",
        isTextureCoordAttribute: false,
        isVertexNormalAttribute: true,
        isLightings: true,
        vsContent: vsLightsContent,
        fsContent: fsLightsContent       
    },
    
    {
        name : "shader_perpixel_lights",
        isTextureCoordAttribute: false,
        isVertexNormalAttribute: true,
        isLightings: true,
        vsContent: vsPerPixelLightsContent,
        fsContent: fsPerPixelLightsContent    
    }
];


var KM = KM || {};              // Namespace
var CANVAS_ID = "kmCanvas";     // Canvas ID
var canvas = null;              // Canvas
var gl = null;                  // WebGL Graphic Context
var gui = null;                 // GUI
var stats = null;               // Stats


function initCanvas()
{
    canvas = document.getElementById( CANVAS_ID );
    console.info( "main.js, initCanvas() : OK" );
}

function initWebGL()
{
    try 
    {
        gl = canvas.getContext( "experimental-webgl" );
    }
    catch( e )
    {
        console.error("main.js, initWebGL() : Can't get webGL context, " + e);
        return;
    }
    console.info( "main.js, initWebGL() : OK" );
}

function initGUI()
{
    gui = new KM.GUI();
    gui.init();
    console.info( "main.js, initGUI() : OK" );
}

function initEngine( iCanvas, iCallback )
{
    engine = new KM.Engine( iCanvas );
    engine.init( iCallback );
    
    console.info( "main.js, initEngine() : OK" );
}

function initStats()
{
    stats = new Stats();
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.top = '0px';
    document.body.appendChild( stats.domElement );
    console.info( "main.js, initStats() : OK" );
}

function initInputsManager( iCanvas, iCamera )
{
    inputsManager = new KM.InputsManager( iCanvas, iCamera );
    inputsManager.bind();
}

function onEngineInitialized()
{
    console.info( "main.js, onEngineInitialized() : OK" );
    
    // Inputs
    initInputsManager( canvas, engine._camera );
    
    // Start engine
    engine.start();
}

/**
 * Entry point of the application
 */
function main()
{
    console.groupCollapsed( "main.js, main() : ");
    
    initCanvas();
    initWebGL();
    initGUI();
    initStats();
    initEngine( canvas, onEngineInitialized );
    
    console.groupEnd();
}

function buildAxes()
{
    // Vertices
    var vertices = [
        // Origin
        0, 0, 0,
        
        // Axe x
        4, 0, 0,
        0, 0, -0.01,
        
        // Axe y
        0, 4, 0,
        0, 0, -0.01,
        
        // Axe z
        0, 0, 4,
        -0.01, 0, 0
    ];
    
    var normals = [
        0, 1, 0,
        
        0, 1, 0,
        0, 1, 0,
        
        0, 1, 0,
        0, 1, 0,
        
        0, 1, 0,
        0, 1, 0
    ];
    
    // Triangles
    var triangles = [];
    triangles.push( new KM.Triangle( 0, 1, 2 ) );
    triangles.push( new KM.Triangle( 0, 3, 4 ) );
    triangles.push( new KM.Triangle( 0, 5, 6 ) );
    
    // Geometry
    var geometry = new KM.Geometry( vertices, normals, triangles );
    
    // Transform
    var transform = new KM.Transform();
    
    // Material
    var material = new KM.Material( "mat_axes_1",
        // Emissive
        new KM.Color( 0.0, 0.0, 0.0 ),
        // Ambient
        new KM.Color( 1.0, 1.0, 1.0 ),
        // Diffuse
        new KM.Color( 1.0, 1.0, 1.0 ),
        // Specular
        new KM.Color( 0, 0, 0 ),
        // Alpha
        1.0 );
    
    // Object3D
    return new KM.Object3D( "axes", geometry, transform, material );
}